package com.example.facility.controller.admin.facility;

import cn.hutool.core.bean.BeanUtil;
import com.example.facility.common.CommonResult;
import com.example.facility.common.PageResult;
import com.example.facility.controller.admin.facility.vo.*;
import com.example.facility.dal.dataobject.facility.FacilityDO;
import com.example.facility.service.facility.FacilityService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.example.facility.common.CommonResult.success;


@RestController
@RequestMapping("/facility/facility")
@Validated
@CrossOrigin
public class FacilityController {

    @Resource
    private FacilityService facilityService;

    /**
     * 创建设备管理
     *
     * @param createReqVO
     * @return
     */
    @PostMapping("/create")
    public CommonResult<Long> createFacility(@RequestBody FacilitySaveReqVO createReqVO) {
        return success(facilityService.createFacility(createReqVO));
    }

    /**
     * 更新设备管理
     *
     * @param updateReqVO
     * @return
     */
    @PostMapping("/update")
    public CommonResult<Boolean> updateFacility(@RequestBody FacilitySaveReqVO updateReqVO) {
        facilityService.updateFacility(updateReqVO);
        return success(true);
    }

    /**
     * 删除设备管理
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public CommonResult<Boolean> deleteFacility(@RequestParam("id") Long id) {
        facilityService.deleteFacility(id);
        return success(true);
    }

    /**
     * 获得设备管理
     *
     * @param id
     * @return
     */

    @GetMapping("/get")
    public CommonResult<FacilityRespVO> getFacility(@RequestParam("id") Long id) {
        FacilityDO facility = facilityService.getFacility(id);
        FacilityRespVO facilityRespVO = new FacilityRespVO();
        BeanUtil.copyProperties(facility, facilityRespVO);
        return success(facilityRespVO);
    }

    /**
     * 获得设备管理分页
     *
     * @param pageReqVO
     * @return
     */
    @PostMapping("/list")
    public CommonResult<List<FacilityDO>> getFacilityPage(@RequestBody FacilityPageReqVO pageReqVO) {
        List<FacilityDO> pageResult = facilityService.getFacilityPage(pageReqVO);
        return success(pageResult);
    }


    /**
     * 根据编号查询一条数据
     *
     * @return
     */
    @PostMapping("/get-by-no")
    public CommonResult<FacilityDO> getByNo(@RequestBody FacilityReqVO reqVO) {
        return success(facilityService.getByNo(reqVO.getName(), reqVO.getType()));
    }

    /**
     * 更新设备管理状态(1 ids 设备ids 必填 ,2 userId 用户id 领用时 必填, 3 type 类型(1领用 2归还) 必填)
     *
     * @param updateReqVO
     * @return
     */
    @PostMapping("/update/status")
    public CommonResult<Boolean> updateFacilityStatus(@RequestBody FacilityUpdateReqVO updateReqVO) {
        facilityService.updateFacilityStatus(updateReqVO);
        return success(true);
    }

}